package cn.javaxc.mvcwork.framework.filters;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;



public class EncodingFilter implements Filter{
	private String m_encoding =  "UTF-8";
	public void destroy(){

	}
	public void init(FilterConfig conf) throws ServletException{
		String val = conf.getInitParameter("encoding");
		if(val!=null) this.m_encoding = val;

	}
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException{
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse) resp;
		request.setCharacterEncoding(m_encoding);
		String origin = request.getHeader("Origin");
        if(origin == null) {
            origin = request.getHeader("Referer");
        }
		response.setHeader("Access-control-Allow-Origin", origin);
		response.setHeader("Access-Control-Allow-Credentials", "true");//true代表允许携带cookie
		chain.doFilter(req, resp);

	}

	


}
